import {
  collectGoods,
  channelProducts
} from "../../../api.js"
Page({
  data: {
    productList: {},
    filterPrice: 0,
    bottomBull: true,
    formlist: {
      token: '',
      page: 1,
      id: '',
      order_by: 1,
      category: 0
    },
    scrollTop: 0,
    sortList: [{
      id: '2',
      name: '最新上架'
    }, {
      id: '3',
      name: '加购次数'
    }, {
      id: '4',
      name: '收藏次数'
    }, {
      id: '5',
      name: '分享次数'
    }, {
      id: '6',
      name: '价格升序'
    }, {
      id: '7',
      name: '价格降序'
    }],
    icon: ['https://document.growthupup.com/arrowbot.png', 'https://document.growthupup.com/arrowtop.png'],
    sortType: false,
    categoryType:false,
    categoryList: [{
      id: 0,
      name: '全部'
    }, {
      id: 1,
      name: '箱包'
    }, {
      id: 2,
      name: '腕表'
    }, {
      id: 3,
      name: '首饰'
    }, {
      id: 4,
      name: '配饰'
    }],
    bottomBull: true
  },
  onLoad: function(options) {
    let business = getApp().globalData.Store.business.id
    let token = getApp().globalData.Store.token;
    var that = this
    that.data.formlist.token = token
    that.data.formlist.id = options.channelId
    console.log(options)
    channelProducts(token, options.channelId, 1).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        that.dealRep(res.data.data)
      }
    })
  },
  onShow: function() {
    getApp().startpage(new Date().getTime())
  },
  dealRep(productList) {
    for (let i = 0, l = productList.data.length; i < l; i++) {
      let data = productList.data[i]
      let productInfo = data.full_name.split(" ")
      if (data.brand_name != null) {
        let brandName = data.brand_name.split("/")
        productList.data[i] = {
          ...data,
          quality: productInfo[0],
          brand: productInfo[1],
          title: productInfo[2],
          brand_name: brandName[0]
        }
      } else {
        productList.data[i] = {
          ...data,
          quality: productInfo[0],
          brand: productInfo[1],
          title: productInfo[2]
        }
      }
    }
    this.setData({
      productList
    })
  },
  /**
   * 修改人：邵群
   * 修改时间：2018-11-28
   * 修改内容：首页的活动展示与跳转
   * */
  collect(e) {
    let product_id = String(e.currentTarget.dataset.id);
    let token = getApp().globalData.Store.token;
    let index = e.currentTarget.dataset.ind;
    collectGoods({
      product_id,
      token
    }).then(res => {
      if (res.data.code == 666) {
        let isCollect = 'productList.data[' + index + '].user_wishlist'
        this.setData({
          [isCollect]: this.data.productList.data[index].user_wishlist == 1 ? 0 : 1
        })
        if (this.data.productList.data[index].user_wishlist == 0) {
          wx.showToast({
            title: '已取消收藏',
            icon: 'none',
          })
        } else if (this.data.productList.data[index].user_wishlist == 1) {
          wx.showToast({
            title: '已成功收藏',
            icon: 'none',
          })
        }
      }
    })
  },
  onHide: function() {
    getApp().endpage(new Date().getTime())
  },
  /*默认按钮*/
  defaultbtn() {
    var that = this
    if (that.data.formlist.order_by != 1) {
      that.setData({
        'formlist.order_by': 1,
        'formlist.category': 0,
        scrollTop: 0
      })
      channelProducts(that.data.formlist.token, that.data.formlist.id, 1).then(res => {
        if (res.data.code == 666) {
          that.dealRep(res.data.data)
        }
      })
    } else {
      that.setData({
        scrollTop: 0
      })
    }
  },
  // 排序下拉
  sortBtn() {
    var that = this
    that.setData({
      sortType: !that.data.sortType,
      categoryType:false
    })
  },
  sortlistBtn(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    if (that.data.formlist.order_by == id) {
      that.setData({
        sortType: false
      })
    } else {
      that.setData({
        'formlist.order_by': id,
        sortType: false,
        scrollTop: 0
      })
      var list = that.data.formlist
      channelProducts(list.token, list.id, 1, list.order_by, list.category).then(res => {
        if (res.data.code == 666) {
          that.dealRep(res.data.data)
        }
      })
    }
  },
  categoryBtn(){
    var that = this
    that.setData({
      categoryType: !that.data.categoryType,
      sortType:false
    })
  },
  categorylistBtn(e){
    var that = this
    var id = e.currentTarget.dataset.id
    if (that.data.formlist.category == id) {
      that.setData({
        categoryType: false
      })
    } else {
      that.setData({
        'formlist.category': id,
        categoryType: false,
        scrollTop: 0
      })
      var list = that.data.formlist
      channelProducts(list.token, list.id, 1, list.order_by, list.category).then(res => {
        if (res.data.code == 666) {
          that.dealRep(res.data.data)
        }
      })
    }
  },
  // 上拉加载
  loadMore(){
    wx.showLoading({
      title: '正在加载',
    })
    var that = this
    var lis = that.data.productList
    var list = that.data.formlist
    if (lis.current_page == lis.last_page){

      wx.showToast({
        title: "没有更多商品了～",
        icon: "none"
      })
      return
    }
    if (that.data.bottomBull) {
      that.setData({
        bottomBull: false
      })
    } else {

      console.log('1a1')
      return
    }
    channelProducts(getApp().globalData.Store.token, list.id, ++lis.current_page, list.order_by, list.category).then(res => {
      if (res.data.code == 666) {          
        wx.hideLoading()
        var liss = that.data.productList
        liss.current_page = lis.current_page
        liss.data = liss.data.concat(res.data.data.data)
        that.dealRep(liss)
        that.setData({
          bottomBull: true
        })
      }
    })
  }
})